package com.robot.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.robot.dao.InterfaceMapper;
import com.robot.dao.TestCaseMapper;
import com.robot.model.Protocol;
import com.robot.model.TestCase;
import com.robot.vo.ResponseMessage;
import com.robot.vo.TablePage;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/10/13
 * Time: 10:47
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Service
@Slf4j
public class InterfaceService {

    @Autowired
    InterfaceMapper interfaceMapper;
    @Autowired
    TestCaseMapper testCaseMapper;

    public ResponseMessage saveInterface(Protocol protocol) {
        try {
            int i = interfaceMapper.addProtocol(protocol);
            if (i != 1) {
                return ResponseMessage.errorResponse("新增组件异常,联系管理员");
            }
            return ResponseMessage.successResponse();
        } catch (Exception e) {
            log.error("新增组件异常", e);
            return ResponseMessage.errorResponse("新增组件异常,联系管理员");
        }
    }
    /**
     * 修改组件信息
     * @param protocol
     * **/
    public ResponseMessage modifyInterface(Protocol protocol) {
        try {
            int i = interfaceMapper.updateProtocol(protocol);
            if (i != 1) {
                return ResponseMessage.errorResponse("修改组件异常,联系管理员");
            }
            return ResponseMessage.successResponse();
        } catch (Exception e) {
            log.error("修改组件异常", e);
            return ResponseMessage.errorResponse("修改组件异常,联系管理员");
        }
    }
    /**
     * 查询组件列表
     * @param protocol
     * **/
    public ResponseMessage interfaceList(Protocol protocol) {
        Page page = PageHelper.startPage(protocol.getPageNum(), protocol.getPageSize(), true);
        List<Protocol> protocolList = interfaceMapper.getProtocolList(protocol);
        TablePage tablePage =new TablePage();
        tablePage.setTotal(page.getTotal());
        tablePage.setRows(protocolList);
        return ResponseMessage.successResponse(tablePage);
    }
    /**
     * 删除接口信息、删除接口下面的所有用例，删除测试计划的信息|(后面说)
     * @param interfaceId
     * @return
     * **/
    @Transactional
    public void delete(Integer interfaceId){
        interfaceMapper.removeProtocol(interfaceId);
        testCaseMapper.removeTestCaseByInterfaceId(interfaceId);
    }
}
